package chattle.shared;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * the Hello entities represent greetings from logged-in users
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "false")
public class ChatMessage implements Serializable
{
	private static final long serialVersionUID = 5934511983136943148L;
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	String key;
	@Persistent
	private String senderId;
	// not persistent, just for data transfer
	public User sender;
	@Persistent
	private String receiverId;
	@Persistent
	private String text;
	@Persistent
	private Date when;

	public ChatMessage(User sender, User receiver, String text, Date when) {
		this.senderId = sender.getUserId();
		this.receiverId = receiver.getUserId();
		this.text = text;
		this.when = when;
	}
	@SuppressWarnings("unused")
	private ChatMessage()
	{}
	
	public String getKey() {
		return key;
	}

	public String getSenderId() {
		return senderId;
	}

	public void setSender(User sender) {
		this.senderId = sender.getUserId();
	}

	public String getReceiverId() {
		return receiverId;
	}

	public void setReceiver(User receiver) {
		this.receiverId = receiver.getUserId();
	}
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}

	public Date getWhen() {
		return when;
	}

	public void setWhen(Date when) {
		this.when = when;
	}
	
	@Override
	public String toString() {
		return when + " " + senderId + " -> " + receiverId + ": " + text;
	}
	
}
